package cn.com.system.controller;

import cn.com.system.pojo.Role;
import cn.com.system.pojo.Staff;
import cn.com.system.service.RoleService;
import cn.com.system.service.StaffService;
import cn.com.system.tools.Constants;
import cn.com.system.tools.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Resource
    private StaffService staffservice;
    @Resource
    private RoleService roleService;

    @RequestMapping("/staffList")
    public String staffList(Model model ,Staff staff,
                            @RequestParam(required=false) String queryname,
                            @RequestParam(required=false) String queryUserRole,
                            @RequestParam(required=false) String pageIndex){
            System.out.println("这是"+queryname);
            System.out.println("这是"+queryUserRole);
            System.out.println("这是"+pageIndex);
            int queryUserRole2 = 0;
            List<Staff> staffList = null;
            int pageSize = Constants.pageSize;
            int currentPageNo = 1;
            if(queryname == null){
                queryname = "";
            }
            if(queryUserRole != null && !queryUserRole.equals("")){
                queryUserRole2 = Integer.parseInt(queryUserRole);
            }
            if(pageIndex != null){
                try{
                    currentPageNo = Integer.parseInt(pageIndex);
                }catch(NumberFormatException e){
                    System.out.println("转换异常");
                }
            }
            HashMap<String,Object>map=new HashMap<String,Object>();
            map.put("r_Name", queryname);
            map.put("s_role", queryUserRole2);
            int totalCount	= staffservice.getUserCount(map);
            PageSupport pages=new PageSupport();
            pages.setCurrentPageNo(currentPageNo);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            if(currentPageNo < 1){
                currentPageNo = 1;
            }else if(currentPageNo > totalPageCount){
                currentPageNo = totalPageCount;
            }
            HashMap<String,Object> map1=new HashMap<String,Object>();
            map1.put("r_Name", queryname);
            map1.put("s_role", queryUserRole2);
            map1.put("currentPageNo", (currentPageNo-1)*pageSize);
            map1.put("pageSize", pageSize);
            System.out.println(map1);
            staffList = staffservice.getUserList(map1);
            System.out.println(staffList);

            model.addAttribute("staffList", staffList);
            List<Role> roleList = null;
            roleList=roleService.getRoleList();
            System.out.println("这是+roleList"+roleList);
            model.addAttribute("roleList", roleList);
            model.addAttribute("queryUserName", queryname);
            model.addAttribute("queryUserRole", queryUserRole2);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", currentPageNo);
            return "system";
    }
    @RequestMapping("/staffMod")
    public String staffMod(Staff staff){
        System.out.println(staff);
        int num=staffservice.staffMod(staff);
        System.out.println(num);
        if (num>0){
            System.out.println(num);
            return "redirect:/staff/staffList";
        }else{
            return "index";
        }
    }
    @RequestMapping("/staffAdd")
    public String staffAdd( ){
        return "add-system";
    }
    @RequestMapping("/staffAdd.do")
    public String staffAddDo(Staff staff ){
        int num=staffservice.addStaff(staff);
        if (num>0){
            return "redirect:/staff/staffList";
        }else{
            return "index";
        }
    }

}

